package org.crazyit.activiti_d4._5_DeleteDeployment;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;

public class DeleteDeployment {

    public static void main(String[] args) {

        //1. 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2. 得到流程存储服务对象
        RepositoryService repositoryService = engine.getRepositoryService();

        //3. 部署一份流程文件与相应的流程图文件
        Deployment dep = repositoryService.createDeployment().addClasspathResource("deleteDeployment.bpmn").deploy();

        //4. 查询流程定义
        ProcessDefinition def = repositoryService.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();

        //5. 启动流程
        engine.getRuntimeService().startProcessInstanceById(def.getId());

        try{

            //删除部署数据失败, 此时会抛出异常, 由于cascade 为false
            repositoryService.deleteDeployment(dep.getId());

        } catch (Exception e) {

            System.out.println("删除失败, 流程开始, 没有设置cascade 为true");

        }

        System.out.println("================分割线================");

        //成功删除部署数据
        repositoryService.deleteDeployment(dep.getId(), true);

    }

}
